﻿using System;
using System.Data;
using System.Collections.ObjectModel;
using Highcharts.Core;
using Highcharts.Core.Appearance;
using Highcharts.Core.Data.Chart;
using Highcharts.Core.PlotOptions;

namespace DemoWebforms
{
    public partial class Sample10 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Exemplo10();
        }

        private void Exemplo10()
        {
            
            //Define the axes
            hcVendas.YAxis.Add(new YAxisItem {title = new Title("Faturamento")});
            hcVendas.XAxis.Add(new XAxisItem { type = AxisDataType.datetime, dateTimeLabelFormats = new DateTimeLabelFormats { day = "%e of %b" } });

            hcVendas.Tooltip = new ToolTip("Highcharts.dateFormat('%e. %b %Y', this.x) +': '+ this.y");

            //Get point collection
            var pointCollection = new PointCollection();
            DataTable dataTable = GetDataTable();
            foreach (DataRow row in dataTable.Rows)
            {
                pointCollection.Add(new Point(Convert.ToInt64((DateTime.Parse(row["date"].ToString()).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds), Convert.ToDouble(row["value"])));
            }
            
            //Add data to serie
            var series = new Collection<Serie> {new Serie {data = pointCollection.ToArray()}};
            hcVendas.PlotOptions = new PlotOptionsLine { dataLabels = new DataLabels { enabled = true } };
            
            //Bind the control
            hcVendas.DataSource = series;
            hcVendas.DataBind();

        }

        private DataTable GetDataTable()
        {
            DataTable dataTable = InitDataTable();
            DataRow row = dataTable.NewRow();
            row["recordID"] = 1;
            row["date"] = new DateTime(2011, 12, 1);
            row["value"] = 5;
            dataTable.Rows.Add(row);
            
            row = dataTable.NewRow();
            row["recordID"] = 2;
            row["date"] = new DateTime(2011, 12, 2);
            row["value"] = 7;
            dataTable.Rows.Add(row);

            row = dataTable.NewRow();
            row["recordID"] = 3;
            row["date"] = new DateTime(2011, 12, 3);
            row["value"] = 9;
            dataTable.Rows.Add(row);

            row = dataTable.NewRow();
            row["recordID"] = 4;
            row["date"] = new DateTime(2011, 12, 4);
            row["value"] = 8;
            dataTable.Rows.Add(row);

            row = dataTable.NewRow();
            row["recordID"] = 5;
            row["date"] = new DateTime(2011, 12, 5);
            row["value"] = 10;
            dataTable.Rows.Add(row);

            return dataTable;
        }

        private DataTable InitDataTable()
        {
            var dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("recordID", typeof(int)));
            dataTable.Columns.Add(new DataColumn("date", typeof(DateTime)));
            dataTable.Columns.Add(new DataColumn("value", typeof(double)));
            return dataTable;
        }
    }
}
